import axios from 'axios'
import qs from 'qs'
import { Message ,Loading} from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'

//import NProgress from 'nprogress'
//axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
var http=axios.create({
  //baseURL: process.env.BASE_API, // api的base_url
  timeout: 50000                  // 请求超时时间
  
});
var loadingInstance;
http.interceptors.request.use(config => {
    //NProgress.start()
    //console.log(config);
    store.dispatch('Loading',true)
   
    if (store.getters.token) {
    
       config.headers['x-lsc-AthToken'] = getToken()//"dsadasdasdsa" // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
    };
    //loadingInstance=Loading.service({target:"contentBox",text:"loading"});
    return config
}, error => {
    return Promise.reject(error)
})

http.interceptors.response.use(response => {/*loadingInstance.close();*/ 
     store.dispatch('Loading',false)
     return response.data;
    }, error => {
    store.dispatch('Loading',false)
    /*console.log('err' + error)// for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.resolve(error.response)*/
  
    //console.log('err' + error)// for debug
    if (error.response.status==666){
       Message({
        message:error.response.data||"没有权限,请联系管理员添加权限！",
        type: 'error',
        duration: 5 * 1000
      });
      return Promise.reject(error)
    }
    if (error.response.status==777){
      Message({
        message:error.response.data||"身份信息失效,请重新登录！",
        type: 'error',
        duration: 5 * 1000
      });
      
      setTimeout(()=>{
        store.dispatch('LogOut').then(() => {
            location.reload()// 为了重新实例化vue-router对象 避免bug
        })
      },1500)
      
      return Promise.reject(error)
    }
    if (!error.response.data.success){
      Message({
        message:error.response.data.err||"系统正忙，请稍后再试！",
        type: 'error',
        duration: 5 * 1000
      })
      /*loadingInstance.close();*/
      return Promise.reject(error)
    }
    
  
}) 

// function checkStatus(response) {
//     //NProgress.done()
//     if (response.status === 200 || response.status === 304) {
//         return response
//     }
//     return {
//         data: {
//             code: -404,
//             message: response.statusText,
//             data: response.statusText,
//         }
//     }
// }

// function checkCode(res) {
//     if (res.data.code !== 200) {
//         alert(res.data.message)
//     }
//     return res
// }

export default {
    post(url, data) {
        return http({
            method: 'post',
            url,
            data: qs.stringify(data),
            timeout: 30000,
            headers: { 
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
        }) 
    },
    get(url, params) {
        return http({
            method: 'get',
            url,
            params,
            timeout: 30000,
            headers: {
               
            }
        }) 
    },postJson(url, data ,responseType) {
        return http({
            responseType:responseType||'json',
            method: 'post',
            url,
            data: data,
            timeout: 30000,
            headers: { 
                'Content-Type': 'application/json;charset=utf-8'
            }
        }) 
    },
    putJson(url, data) {
        return http({
            method: 'put',
            url,
            data: data,
            timeout: 30000,
            headers: { 
                'Content-Type': 'application/json;charset=utf-8'
            }
        }) 
    },
    del:function(url){
         return http({
            method: 'delete',
            url ,
            timeout: 30000 
        }) 
    },
    put:function(url, data){
         return http({
            method: 'put',
            url,
            data: qs.stringify(data),
            timeout: 30000,
            headers: { 
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
        }) 
    },
    report:function(url,data){
        var instance = axios.create({ 
            timeout: 50000,
            responseType:'arraybuffer'
        });
        instance.interceptors.request.use(config => {
            if (store.getters.token) {
               config.headers['x-lsc-AthToken'] = getToken()
            };
            return config
        }, error => {
            return Promise.reject(error)
        });
        return instance({
            method: 'post',
            url,
            data: qs.stringify(data),
            headers: { 
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
            },
            
        }) 
        
    }
}
